package com.application.sistercityapplication.cityinformationpage;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.sistercityapplication.dto.InformationDto;
import com.application.sistercityapplication.dto.InformationMasterDto;
import com.application.sistercityapplication.listadapter.InformationDetailListAdapter;
import com.application.sistercityapplication.main.R;
import com.application.sistercityapplication.model.Magazine;
import com.application.sistercityapplication.utils.SwipeDismissListViewTouchListener;
import com.roscopeco.ormdroid.Entity;
import com.squareup.picasso.Picasso;

public class CityInformationListAdapter extends BaseAdapter {
	private List<InformationMasterDto> _listCityInformation;
	private Context _context;
	private ViewHolderListInforList viewHolder = null;
	private InformationMasterDto mag;
	private Activity _activity;
	private FrameLayout _frame;
	private boolean isDismiss = false;
	private String c_id;
	//private String pattern = "(id\\=\\\"customerregistrationForm\\:j_idt[0-9]*\\\"){1}.*((value\\=\\\"Missing fingerprint\\\")|(value\\=\\\"Get fingerprint\\\")){1}";
    
	public CityInformationListAdapter(List<InformationMasterDto> list,Context con,Activity ac,FrameLayout frame,String country_id){
		this._listCityInformation = list;
		this._context = con;
		this._activity = ac;
		this._frame = frame;
		this.c_id = country_id;
//		String result = "Hello".split("\\\"")[1];
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _listCityInformation.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return _listCityInformation.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		// TODO Auto-generated method stub
		
		if(convertView== null){
			viewHolder = new ViewHolderListInforList();
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.city_list_each_item, null);
			viewHolder.imageItem = (ImageView) convertView.findViewById(R.id.image_item);
			viewHolder.textItem = (TextView) convertView.findViewById(R.id.text_item);
			viewHolder.smallText = (TextView) convertView.findViewById(R.id.smallText);
			viewHolder.click_relative = (RelativeLayout) convertView.findViewById(R.id.click_relative);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolderListInforList) convertView.getTag();
		}
		mag = _listCityInformation.get(position);
		try{
			/*
			 * getImage from dummy
			 */
			Picasso.with(_context).load(mag.getImageDrawable()).into(viewHolder.imageItem);
			viewHolder.textItem.setText(mag.getName());
			viewHolder.smallText.setText(mag.getName());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		viewHolder.click_relative.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("ResourceAsColor")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				AbstractWheel logoShelfListView = (AbstractWheel) _activity.findViewById(R.id.flagLogoList);
//				logoShelfListView.setVisibility(View.GONE);
				mag = _listCityInformation.get(position);
				LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				_frame.removeAllViews();
				View informationView = inflater.inflate(R.layout.activity_city_information_detail_main, null);
				_frame.addView(informationView);
				ImageView back_icon = (ImageView) informationView.findViewById(R.id.back_icon);
				TextView txtView = (TextView) informationView.findViewById(R.id.textView1);
				mag = _listCityInformation.get(position);
				txtView.setText(mag.getName());
				List<InformationDto> list = mag.getInformations();
				final ListView informationDetailListView = (ListView) informationView.findViewById(R.id.informationDetailListView);
				final InformationDetailListAdapter listAdapter = new InformationDetailListAdapter(list,_context,_activity);
				SwipeDismissListViewTouchListener touchListener =
		                new SwipeDismissListViewTouchListener(
		                		informationDetailListView,
		                        new SwipeDismissListViewTouchListener.DismissCallbacks() {
		                            
		                			Magazine magazine = new Magazine();
		                			@Override
		                            public boolean canDismiss(int position) {
		                            	boolean isValid = false;
		                            	InformationDto dto = (InformationDto) informationDetailListView.getItemAtPosition(position);
		                            	magazine  = Entity.query(Magazine.class).where("magId").eq(dto.getMagId()).execute();
		                    			if(magazine!=null){
		                    				isValid = true;
		                    			}else{
		                    				isValid = false;
		                    			}
		                                return isValid;
		                            }

		                            @Override
		                            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
		                            	
		                                for (int position : reverseSortedPositions) {
		                                    //mAdapter.remove(mAdapter.getItem(position));
		                                	//docAdapter.
		                                	View view = informationDetailListView.getChildAt(position);
		                                	final ImageView deleteBt =  (ImageView) view.findViewById(R.id.deleteBookBtn);
		                                	final ImageView dowloadBt =  (ImageView) view.findViewById(R.id.downLoadBookBtn);
		                                	if(!isDismiss){
		                                	deleteBt.setVisibility(View.VISIBLE);
		                                	isDismiss = true;
		                                	deleteBt.setOnClickListener(new OnClickListener() {
												
												@Override
												public void onClick(View v) {
													magazine.delete();
													deleteBt.setVisibility(View.GONE);
													dowloadBt.setVisibility(View.VISIBLE);
													listAdapter.notifyDataSetChanged();
												}
											});
		                                	}else{
		                                	deleteBt.setVisibility(View.GONE);
			                                isDismiss = false;	
		                                	}
		                                }
		                                listAdapter.notifyDataSetChanged();
		                            }
		                        });
				informationDetailListView.setOnTouchListener(touchListener);
				informationDetailListView.setAdapter(listAdapter);
				
				
				
				back_icon.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
//						AbstractWheel logoShelfListView = (AbstractWheel) _activity.findViewById(R.id.flagLogoList);
//						logoShelfListView.setVisibility(View.VISIBLE);
						LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						_frame.removeAllViews();
						View informationView = inflater.inflate(R.layout.general_info_list, null);
						_frame.addView(informationView);
						ImageView back_icon = (ImageView) informationView.findViewById(R.id.back_icon);
						ProgressBar progress = (ProgressBar) informationView.findViewById(R.id.progress);
						progress.setVisibility(View.INVISIBLE);
						back_icon.setVisibility(View.INVISIBLE);
//						List<InformationMasterDto> list = JSONParserForGetList.getInstance().getInformations(c_id);
						
						ListView informationListView = (ListView) informationView.findViewById(R.id.informationListView);
						CityInformationListAdapter adp = new CityInformationListAdapter(_listCityInformation, _context,_activity, _frame,c_id);
						informationListView.setAdapter(adp);

					}
				});
			}
		});
		
		return convertView;
	}

	public class ViewHolderListInforList {
		public ImageView imageItem;
		public TextView textItem;
		public TextView smallText;
		public RelativeLayout click_relative;
	}
}
